
//
//  NewStoreRemarkView.m
//  Yanwei
//
//  Created by weijunjie on 2018/10/24.
//  Copyright © 2018年 DCloud. All rights reserved.
//

#import "NewStoreRemarkView.h"
#import "GoodsModel.h"
#import "UIView+XWAddForRoundedCorner.h"
#import "MuchStoreRequestManager.h"
#import "DeviceDataInfo.h"
#import "UIView+Util.h"


@interface NewStoreRemarkView()
@property (nonatomic, strong) UILabel *groupNumberLabel;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UILabel *groupPurshaseTimeLabel;
@property (nonatomic, strong) UIButton *remarkBtn;
@property (nonatomic, strong) UILabel *rightArrowLabel;
@property (nonatomic, strong) UILabel *rightLabel;
@property (nonatomic, assign, getter=isOver) BOOL over;

/** 定时器 */
@property (nonatomic, strong) CADisplayLink *displayLink;

/** 拼团页面团购人数 */
@property (nonatomic, strong) UILabel *groupPeopleNumberLabel;
/** 拼团页面剩余时间 */
@property (nonatomic, strong) UILabel *groupPeoplePurshaseTimeLabel;
@end
@implementation NewStoreRemarkView
/*

- (void)setGoodsModel:(GoodsModel *)goodsModel {
    _goodsModel = goodsModel;
    
    if (self.isGroupPurshase) {
        self.groupPeopleNumberLabel.text = [NSString stringWithFormat:@"限时%@人团",goodsModel.showGroupPeopleNumber];
        self.backgroundColor = NewStoreNoSupportMarkedColor;
    } else {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 20;
        self.layer.borderWidth = 0.5;
        self.layer.borderColor = Color175030.CGColor;
        if (goodsModel.isRemind == 0) { // 已经订阅
            [self.remarkBtn setBackgroundImage:[UIImage imageNamed:@"store_detail_subscribe"] forState:UIControlStateNormal];
            self.remarkBtn.userInteractionEnabled = NO;
        } else {
            [self.remarkBtn setBackgroundImage:[UIImage imageNamed:@"store_detail_remark"] forState:UIControlStateNormal];
            self.remarkBtn.userInteractionEnabled = YES;
        }
        
        self.groupNumberLabel.text = [NSString stringWithFormat:@"%ld人拼团价",(long)goodsModel.groupPeopleNumber];
        NSString *groupPrice = goodsModel.groupPrice;
        if (goodsModel.groupStyleType == NewStoreDetailGroupHeaderType) { // 团长团
            groupPrice = goodsModel.groupHeaderPrice;
        }
        self.priceLabel.text = [NSString stringWithFormat:@"¥%@",groupPrice];
    }

    [self updateTimeInfo];
    [self.displayLink invalidate];
    self.displayLink = nil;
    [self displayLink];
}

#pragma mark - event
- (void)remarkBtnAction:(UIButton *)btn {
    if (!USER.isLogined) {
        if (self.toLogin) {
            self.toLogin();
        }
        return;
    }    
    self.goodsModel.isRemind = 0;
    [btn setBackgroundImage:[UIImage imageNamed:@"store_detail_subscribe"] forState:UIControlStateNormal];
    btn.userInteractionEnabled = NO;
    [StoreRequestManager storeRemindOpenGroupPurshaseWithSpuid:self.goodsModel.sid sucess:^(id data) {
        
        // 添加提醒事件到日历中
        NSString *title = [NSString stringWithFormat:@"开团:%@",self.goodsModel.title];
        [DeviceDataInfo saveEventToCalendarWithTitle:title remark:nil timeStamp:self.goodsModel.openTime];
        // 订阅成功
        NSString *message = @"我们会在开团前1小时给您短信通知";
        if (data != nil) {
            message = data;
        }
        [[StoreAlertView alertViewWithSize:CGSizeMake(282, 151) title:@"加入提醒成功" messages:@[message] btnTitle:@"确定" btnAction:nil] show];
    } fail:^(NSString *error) {
        [SVProgressHUD showErrorWithStatus:error];
        [btn setBackgroundImage:[UIImage imageNamed:@"store_detail_remark"] forState:UIControlStateNormal];
        btn.userInteractionEnabled = YES;
        self.goodsModel.isRemind = 1;
    }];
    
}

#pragma mark - 懒加载
- (UILabel *)groupNumberLabel {
    if (!_groupNumberLabel) {
        _groupNumberLabel = [[UILabel alloc] init];
        _groupNumberLabel.textColor = Color34;
        _groupNumberLabel.font = APPFONT(12);
        [self addSubview:_groupNumberLabel];
        [_groupNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(15);
            make.top.equalTo(self).offset(4);
        }];
    }
    return _groupNumberLabel;
}

- (UILabel *)priceLabel {
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc] init];
        _priceLabel.textColor = Color175030;
        _priceLabel.font = APPBOLDPINGFANGFONT(14);
        [self addSubview:_priceLabel];
        [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.groupNumberLabel.mas_right).offset(3);
            make.centerY.equalTo(self.groupNumberLabel);
        }];
    }
    return _priceLabel;
}

- (UILabel *)groupPurshaseTimeLabel {
    if (!_groupPurshaseTimeLabel) {
        _groupPurshaseTimeLabel = [[UILabel alloc] init];
        _groupPurshaseTimeLabel.textColor = Color34;
        if (ScreenWidth == 320) {
            _groupPurshaseTimeLabel.font = APPFONT(10);
        } else {
            _groupPurshaseTimeLabel.font = APPFONT(12);
        }
        [self addSubview:_groupPurshaseTimeLabel];
        [_groupPurshaseTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.groupNumberLabel);
            make.bottom.equalTo(self).offset(-4);
        }];
    }
    return _groupPurshaseTimeLabel;
}

- (UIButton *)remarkBtn {
    if (!_remarkBtn) {
        _remarkBtn = [[UIButton alloc] init];
        _remarkBtn.adjustsImageWhenHighlighted=NO;
        [self addSubview:_remarkBtn];
        [_remarkBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-15);
            make.centerY.equalTo(self);
            make.width.mas_equalTo(60);
            make.height.mas_equalTo(20);
        }];
        [_remarkBtn addTarget:self action:@selector(remarkBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _remarkBtn;
}

- (CADisplayLink *)displayLink {
    if (!_displayLink) {
        _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateTimeInfo)];
        [_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
        [_displayLink setFrameInterval:1];
    }
    return _displayLink;
}

- (UILabel *)rightArrowLabel {
    if (!_rightArrowLabel) {
        _rightArrowLabel = [[UILabel alloc] init];
        _rightArrowLabel.textColor = Color34;
        _rightArrowLabel.textAlignment = NSTextAlignmentRight;
        _rightArrowLabel.text = @"\uE682";
        _rightArrowLabel.font = ICONFONT(14);
        [self addSubview:_rightArrowLabel];
        [_rightArrowLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-15);
            make.centerY.equalTo(self);
        }];
        __weakConfigure
        [_rightArrowLabel tapUpWithBlock:^(UIView *v) {
            if (__weakSelf.jumpToGroupDetailBlock) {
                __weakSelf.jumpToGroupDetailBlock();
            }
        }];
    }
    return _rightArrowLabel;
}
- (UILabel *)rightLabel {
    if (!_rightLabel) {
        _rightLabel = [UILabel new];
        _rightLabel.textColor = Color34;
        _rightLabel.textAlignment = NSTextAlignmentRight;
        _rightLabel.font = APPBOLDPINGFANGFONT(14);
        _rightLabel.text = @"去拼团";
        [self addSubview:_rightLabel];
        [_rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.rightArrowLabel.mas_left).with.offset(- 3);
            make.centerY.equalTo(self.rightArrowLabel);
        }];
        __weakConfigure
        [_rightLabel tapUpWithBlock:^(UIView *v) {
            if (__weakSelf.jumpToGroupDetailBlock) {
                __weakSelf.jumpToGroupDetailBlock();
            }
        }];
    }
    return _rightLabel;
}

#pragma mark - event
- (void)updateTimeInfo {
    
    if (self.contentVC == nil) {
        [self.displayLink invalidate];
        self.displayLink = nil;
        return;
    }
    
    
    if (self.isGroupPurshase) { // 拼团商品详情
        if ([self.goodsModel isInvalid] || self.goodsModel.groupType != 5) {
            if (self.isOver) {
                return;
            }
            self.over = YES;
            [self.displayLink invalidate];
            self.displayLink = nil;
            if (self.canGroupPurshaseBlock) {
                self.canGroupPurshaseBlock();
            }
            self.groupPeoplePurshaseTimeLabel.text = @"距结束：00:00:00";
        } else {
            NSRange range = [self.goodsModel.showGroupCountDownOpenTime rangeOfString:@":"];
            NSString *showStr = [self.goodsModel.showGroupCountDownOpenTime stringByReplacingCharactersInRange:range withString:@"时"];
            showStr = [showStr stringByReplacingOccurrencesOfString:@":" withString:@"分"];
            showStr = [showStr stringByReplacingOccurrencesOfString:@"后开始" withString:@"秒"];
            self.groupPeoplePurshaseTimeLabel.text = [NSString stringWithFormat:@"距结束：%@",showStr];
        }
    } else {
        if ([self.goodsModel isInvalid] || self.goodsModel.groupType == 5) {
            self.groupPurshaseTimeLabel.text = @"拼团购买更划算";
            self.rightLabel.hidden = NO;
            self.rightArrowLabel.hidden = NO;
            self.remarkBtn.hidden = YES;
            if (self.canGroupPurshaseBlock) {
                self.canGroupPurshaseBlock();
            }
        } else {
            self.rightLabel.hidden = YES;
            self.rightArrowLabel.hidden = YES;
            self.groupPurshaseTimeLabel.text = self.goodsModel.showGroupCountDownOpenTime;
            self.remarkBtn.hidden = NO;
        }
    }
    
}

- (UILabel *)groupPeopleNumberLabel {
    if (!_groupPeopleNumberLabel) {
        _groupPeopleNumberLabel = [[UILabel alloc] init];
        _groupPeopleNumberLabel.textColor = Color175030;
        _groupPeopleNumberLabel.font = APPBOLDPINGFANGFONT(15);
        if (ScreenWidth == 320) {
            _groupPeopleNumberLabel.font = APPBOLDPINGFANGFONT(15);
        }
        [self addSubview:_groupPeopleNumberLabel];
        [_groupPeopleNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(15);
            make.top.bottom.equalTo(self);
        }];
    }
    return _groupPeopleNumberLabel;
}

- (UILabel *)groupPeoplePurshaseTimeLabel {
    if (!_groupPeoplePurshaseTimeLabel) {
        _groupPeoplePurshaseTimeLabel = [[UILabel alloc] init];
        _groupPeoplePurshaseTimeLabel.textColor = Color34;
        _groupPeoplePurshaseTimeLabel.font = APPFONT(12);
        if (ScreenWidth == 320) {
            _groupPeoplePurshaseTimeLabel.font = APPFONT(10);
        }
        [self addSubview:_groupPeoplePurshaseTimeLabel];
        [_groupPeoplePurshaseTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-15);
            make.top.bottom.equalTo(self);
        }];
    }
    return _groupPeoplePurshaseTimeLabel;
}

- (void)dealloc {
    [self.displayLink invalidate];
    self.displayLink = nil;
}
*/
@end
